package com.baozhun.mall.common.util

import android.app.Activity
import com.baozhun.mall.common.BuildConfig
import com.baozhun.mall.common.model.bean.GoodsInfoBean
import com.baozhun.mall.common.model.enums.GoodsType
import com.baozhun.mall.common.util.config.SDKConfig
import com.umeng.socialize.Config
import com.umeng.socialize.ShareAction
import com.umeng.socialize.UMShareListener
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMImage
import com.umeng.socialize.media.UMMin

/**
 * @Title：
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2023/11/21
 * @Version：1.0.0
 */
object ShareUtil {
    /**
     * 分享商品到微信小程序
     */
    fun shareGoodsToMINIApp(activity: Activity, goodsInfoBean: GoodsInfoBean?, goodsType: GoodsType?, shareSuccessBack: (() -> Unit)? = null) {
        val umMin = UMMin(goodsInfoBean?.share_url.toString()) //跳转的url
        umMin.setThumb(UMImage(activity, goodsInfoBean?.goods_cover.toString()))
        umMin.title = "【全网卖，巨额返】" + goodsInfoBean?.goods_name.toString()
        umMin.description = goodsInfoBean?.goods_name.toString()
        umMin.path = "/pages/product/detail/index?id=" + goodsInfoBean?.id + "&goods_type=" + goodsType?.type
        umMin.userName = SDKConfig.WX_MINI_APP_ORIGIN_ID
        ShareAction(activity)
            .withMedia(umMin)
            .setPlatform(SHARE_MEDIA.WEIXIN)
            .setCallback(object : UMShareListener {
                override fun onStart(p0: SHARE_MEDIA?) {
                }

                override fun onResult(p0: SHARE_MEDIA?) {
                    shareSuccessBack?.invoke()
                }

                override fun onError(p0: SHARE_MEDIA?, p1: Throwable?) {
                }

                override fun onCancel(p0: SHARE_MEDIA?) {
                }

            }).share()
    }

    /**
     * 分享连单购买到微信小程序
     */
    fun shareRequestContinueBuyToMINIApp(
        activity: Activity,
        shareUrl: String?,
        goodsCover: String?,
        goodsName: String?,
        goodsId: String?,
        shareOrderId: String?,
        shareSuccessBack: (() -> Unit)? = null
    ) {
        val umMin = UMMin(shareUrl) //跳转的url
        umMin.setThumb(UMImage(activity, goodsCover))
        umMin.title = "【低价购，还返利】"
        umMin.description = "【低价购，还返利】"
        if (BuildConfig.DEBUG)
            Config.setMiniPreView()
        umMin.path = "/pages/home/index?share_order_id=$shareOrderId"
        umMin.userName = SDKConfig.WX_MINI_APP_ORIGIN_ID
        ShareAction(activity)
            .withMedia(umMin)
            .setPlatform(SHARE_MEDIA.WEIXIN)
            .setCallback(object : UMShareListener {
                override fun onStart(p0: SHARE_MEDIA?) {
                }

                override fun onResult(p0: SHARE_MEDIA?) {
                    shareSuccessBack?.invoke()
                }

                override fun onError(p0: SHARE_MEDIA?, p1: Throwable?) {
                }

                override fun onCancel(p0: SHARE_MEDIA?) {
                }

            }).share()
    }
}